//
//  TWAnimationTool.swift
//  EyepetizerImitative
//
//  Created by 田伟 on 2017/3/31.
//  Copyright © 2017年 个人. All rights reserved.
//

import UIKit

protocol AnimationToolPresentDelegate : NSObjectProtocol {
    func startRect(indexPath : IndexPath) -> CGRect                 // 获取小图的frame        => 精选页中的cell能提供
    func imageView(indexPath : IndexPath) -> UIImageView            // 临时的imageview
}

protocol AnimationToolDismissDelegate : NSObjectProtocol {
    func dismissImageView(indexPath : IndexPath) -> UIImageView
}

class TWAnimationTool: NSObject {
    // 标记是否是弹出
    fileprivate var isPresented : Bool = false
    // 通过点击cell时传递过来，用来获取小图的frame创建imageView等
    var indexPath : IndexPath?
    // 协议代理属性
    var presentedDelegate : AnimationToolPresentDelegate?
    var dismissDelegate : AnimationToolDismissDelegate?
}

extension TWAnimationTool : UIViewControllerTransitioningDelegate {
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = true
        return self
    }
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = false
        return self
    }
}

extension TWAnimationTool : UIViewControllerAnimatedTransitioning {
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.5
    }
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        isPresented ? animationForPresentedView(using: transitionContext) : animationForDismissView(using: transitionContext)
    }
}

extension TWAnimationTool {
    // 抽出弹出的动画
    fileprivate func animationForPresentedView(using transitionContext: UIViewControllerContextTransitioning) {
        guard let indexPath = indexPath else {
            return
        }
        guard let presentedDelegate = presentedDelegate else {
            return
        }
        // 取出弹出的View.key是To
        let presentedView = transitionContext.view(forKey: UITransitionContextViewKey.to)!
        // 将presentedView添加到containerView中去
        transitionContext.containerView.addSubview(presentedView)
        
        // 获取执行动画的imageView
        let startRect = presentedDelegate.startRect(indexPath: indexPath)
        let imageView = presentedDelegate.imageView(indexPath: indexPath)
        // 获取上下文将imageView添加进去————设置imageView的父控件
        transitionContext.containerView.addSubview(imageView)
        imageView.frame = startRect
        
        // 执行动画
        presentedView.frame = CGRect(x: 0, y: startRect.origin.y, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
//        presentedView.backgroundColor = UIColor.black
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            // 动画完成时的frame
            imageView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: VideoCellHeight)
            presentedView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
        }) { (_) in
            imageView.removeFromSuperview()
//            transitionContext.containerView.backgroundColor = UIColor.orange // 在黑色背景上实行动画，默认没有颜色
            // 告诉上下文已经完成动画
            transitionContext.completeTransition(true)
        }
    }
    // 抽出消失的动画
    fileprivate func animationForDismissView(using transitionContext: UIViewControllerContextTransitioning) {
        guard let dismissDelegate = dismissDelegate else {
            return
        }
        guard let presentedDelegate = presentedDelegate else {
            return
        }
        // 取出消失的View.key是from
        let dismissView = transitionContext.view(forKey: UITransitionContextViewKey.from)!
        dismissView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
        dismissView.removeFromSuperview()           // 使用imageView做动画，dismissView不需要,直接移除
        // 获取执行动画的imageView
        let imageView = dismissDelegate.dismissImageView(indexPath: indexPath!)
        imageView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: VideoCellHeight)
        
        // 获取上下文将imageView添加进去————设置imageView的父控件
        transitionContext.containerView.addSubview(imageView)
        
        // 执行动画
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            imageView.frame = presentedDelegate.startRect(indexPath:self.indexPath!)
//            dismissView.frame = CGRect(x: 0, y: imageView.frame.origin.y, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
        }) { (_) in
            imageView.removeFromSuperview()
//            dismissView.removeFromSuperview()
            // 告诉上下文已经完成动画
            transitionContext.completeTransition(true)
        }
    }
}
